/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui;

import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.JEditorPane;

import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.primitives.Attribute;
import dgbgui.modeldocument.primitives.AttributeType;
import dgbgui.modeldocument.primitives.Conceptron;
import dgbgui.modeldocument.primitives.ConceptronType;
import dgbgui.modeldocument.primitives.Graph;
import dgbgui.selection.SelectionManager;

public class SelectionInspector implements Observer {

    
    public SelectionInspector() {
        
        // Create the text area that will be sued to display the 
        // inspector information
        m_textArea = new JEditorPane();
        m_textArea.setEditable(false);
    }
    
    public void update(Observable source, Object data) {
        
        if ( source instanceof SelectionManager ) {
            assert ( data instanceof SelectionManager.ObservableData );
            // We're not concerned with the actual added/removed selection,
            // we just get the current selection and display that
            

            if ( SelectionManager.getInstance().size(Query.class) >0  ) {
                String inspectorString = "";
                Set<Object> selectedNodes = SelectionManager.getInstance().getSelection(Query.class);
                // Display each node
                int index = 0;
                for ( Object node : selectedNodes ) {
                    if ( index != 0 ) {
                        inspectorString += "\n";
                    }
                    inspectorString += 
                        ((Query)node).getDefinition().getTypeName() + ",\"" + 
                        ((Query)node).getLabel() + "\" ->  " + 
                        ((Query)node).getDefinition().getdgbQLString()+ "";
                    ++index;
                }
                m_textArea.setText(inspectorString);
            }
            // TODO: Look at making the selection manager a hash set only, without 
            // actually storing the class type. The class type could be filtered
            // at run-time? Then we could juts check for ConceptronType.class here
            else if ( SelectionManager.getInstance().size(ConceptronType.class) > 0  ) {
                Set<Object> selection = SelectionManager.getInstance().getSelection(ConceptronType.class);
                int index = 0;
                String inspectorString = "";
                for ( Object obj : selection ) {
                    if ( index != 0 ) {
                        inspectorString += "\n";
                    }
                    ConceptronType type = (ConceptronType)obj;
                    inspectorString +=
                        "Name: \"" + type.m_name + "\""
                        + " ID: " + type.m_id 
                        + " Atributes: ";
                    int index2 = 0;
                    if ( type.m_attributeTypes.length == 0 ) {
                        inspectorString += "None";
                    }
                    for ( AttributeType attrType : type.m_attributeTypes) {
                        if ( index2 != 0 ) {
                            inspectorString += ",";
                        }
                        inspectorString += "\"" + attrType.m_name + "\"";
                        ++index2;
                    }   
                    ++index;
                }
                m_textArea.setText(inspectorString);
            }
            else if (SelectionManager.getInstance().size(Conceptron.class) > 0  ) { 

                Set<Object> selection = SelectionManager.getInstance().getSelection(Conceptron.class);

                int index = 0;
                String inspectorString = "";
                for ( Object obj : selection ) {
                    if ( index != 0 ) {
                        inspectorString += "\n";
                    }
                    Conceptron instance = (Conceptron)obj;
                    ConceptronType conceptronType = ModelDocument.getInstance().getConceptronType(instance);
                    inspectorString += "Type: \"" + conceptronType.m_name + "\"";
                    inspectorString += " Instance ID: " + instance.m_id;
                    inspectorString += " Attributes: \n";
                    for ( Attribute attr : instance.m_attributes ) {
                        AttributeType attributeType = ModelDocument.getInstance().getAttributeType(attr);
                        inspectorString += "  \"" + attributeType.m_name + "\" = \"" + attr.m_value  + "\"\n";
                    }   
                    ++index;
                }
                m_textArea.setText(inspectorString);
            }
            else if ( SelectionManager.getInstance().size(Graph.class) > 0 ) {
                Set<Object> selection = SelectionManager.getInstance().getSelection(Graph.class);
                int index = 0;
                String inspectorString = "";
                for ( Object obj : selection ) {
                    if ( index != 0 ) {
                        inspectorString += "\n";
                    }
                    Graph g = (Graph)obj;
                    inspectorString += "Graph: Vertices: " + g.vertexSet().size() + " Edge: " + g.edgeSet().size();
                    ++index;
                }
                m_textArea.setText(inspectorString);
            }
            else {
                m_textArea.setText("");
            }
        }
    }
    
    public JComponent getComponent() {
        return m_textArea;
    }

    JEditorPane m_textArea;
    
}
